package com.anhuanjia.tools.eao.controller;

import java.util.Date;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoVersion;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.utility.StringUtil;
/**
 * 版本信息操作类
 * @author Administrator
 *
 */
@Alias(value = "version")
public class VersionUI extends UIFacade {
	/**
	 * 添加/更改内容表
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String loginUser ="";
		loginUser = Long.toString(Member.getUID());
		Date data =new Date();
		Transaction transaction = new Transaction();
		EaoVersion entity = new EaoVersion();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			String uid = UUID.randomUUID().toString();
			entity.setId(uid);
			
			entity.setMemberId(loginUser);  //暂时设置为空
			entity.setCreateTime(data);
			entity.setUpdateTime(data);
			entity.setIsDelete(0); //默认0否
			entity.setOperator(loginUser); //关联登录人
			transaction.insert(entity);
			if (transaction.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		} else {
			DataTable dt = new Q("select * from eao_version where Id = ? ", id).fetch();
			EaoVersion old = new EaoVersion();
			old.setValue(dt.get(0));
			old.setValue(this.Request);
			old.setUpdateTime(data);
			old.setOperator(loginUser);//关联登录人，暂时设置为空
			transaction.update(old);
			if (transaction.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		}
	}
	
	/**
	 * 查询 id查询单条记录
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String searchKey = $V("SearchKey");
		if (StringUtils.isEmpty(searchKey)) {
			f("请输入搜索条件！");
			return;
		}
		Q q = new Q("select * from eao_version where Id = ? ", searchKey);
		$S("data", q.fetch());
	}
	
	/**
	 * 查询 查询所有
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		Q q = new Q("select * from eao_version");
		
		$S("data", q.fetch());
	}
	
	/**
	 * 根据id删除
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteByid() {
		String id = $V("Id");
		if (!StringUtil.isNotEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_version  where ").in("Id", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}
}
